// Created by wangliang on 2025/2/25.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../base/base_view_model.dart';

mixin LoadingMixin on BaseViewModel {
  OverlayEntry? _overlayEntry;

  void showLoading() {
    if (_overlayEntry != null) {
      return;
    }
    _overlayEntry ??= OverlayEntry(
      builder: (context) => buildLoading(),
    );
    WidgetsBinding.instance.addPostFrameCallback((_) {
      Overlay.of(context).insert(_overlayEntry!);
    });
    safeBuildView();
  }

  void hideLoading() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _overlayEntry?.remove();
      _overlayEntry = null;
    });
    safeBuildView();
  }

  Widget buildLoading() {
    return GestureDetector(
      onTap: () {},
      child: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        alignment: Alignment.center,
        color: Colors.transparent,
        child: Container(
          width: 60,
          height: 60,
          decoration:
              const BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(10)), color: Color(0x9f000000)),
          child: const CupertinoActivityIndicator(
            color: Colors.white,
          ),
        ),
      ),
    );
  }
}
